/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BE.Song;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author notandi
 */
public class SongTableModel extends AbstractTableModel
{

    public static final int TITLE_COLUMN = 0;
    public static final int ARTIST_COLUMN = 1;
    public static final int LENGTH_COLUMN = 2;
    public static final int GENRE_COLUMN = 3;
    public static final int FAVORITE_COLUMN = 4;
    
    

    private final String[] headers =
    {
        "Title", "Artist", "Length", "Genre", "Is Favorite"
    };
    private final Class[] columnTypes =
    {
        String.class, String.class, Integer.class, String.class, Boolean.class
    };

    private List<Song> songs;

    public SongTableModel(List<Song> initialSongs)
    {
        songs = initialSongs;
        
    }

    public SongTableModel()
    {
        songs = new ArrayList<>();
        
    }

    @Override
    public int getRowCount()
    {
        return songs.size();
    }

    @Override
    public int getColumnCount()
    {
        return headers.length;
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        Song s = songs.get(row);
        switch (col)
        {
            case TITLE_COLUMN:
                return s.getTitle();
            case ARTIST_COLUMN:
                return s.getArtistId();
            case LENGTH_COLUMN:
                return s.getDuration();
            case GENRE_COLUMN:
                return s.getCategoryId();
            case FAVORITE_COLUMN:
                return s.getFavorite();
        }
        return null;
    }
 @Override
    public String getColumnName(int col)
    {
        return headers[col];
    }

    @Override
    public Class<?> getColumnClass(int col)
    {
        return columnTypes[col];
    }

    @Override
    public boolean isCellEditable(int row, int col)
    {
        return (col != TITLE_COLUMN && col != ARTIST_COLUMN && col != LENGTH_COLUMN && col != GENRE_COLUMN);
    }

    @Override
    public void setValueAt(Object value, int row, int col)
    {
        Song s = songs.get(row);
        switch (col)
        {

            case FAVORITE_COLUMN:
                s.setFavorite(Boolean.valueOf(value.toString()));
                break;
        }
        // Fire this event to trigger the tableModelListener.
        fireTableRowsUpdated(row, row);
    }

    public Song getSong(int row)
    {
        return songs.get(row);
    }

    public void setSongList(List<Song> songList)
    {
        songs = songList;
        fireTableDataChanged();
    }    

}
